import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'dart:convert';
import 'dart:io';
import 'package:app_tracking_transparency/app_tracking_transparency.dart';

import 'package:encrypt/encrypt.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';


import 'package:package_info_plus/package_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:scanner/page_routes.dart';

class WJInitLogic extends GetxController {
  /// 网络监听
  final Connectivity _connectivity = Connectivity();

  @override
  void onInit() {
    super.onInit();
    EasyLoading.instance.maskType = EasyLoadingMaskType.black; // 全局禁止交互
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      _initSdkAndLogin();
    });
  }


  _initSdkAndLogin() async {
    //先进行网络开启
    final resultList = await _connectivity.checkConnectivity();
    if (resultList.contains(ConnectivityResult.none)) {
      //无网情况
       var isPing = await _pingTest('https://www.baidu.com');
       if(isPing == true){
          _initSdkAndLogin();
          return;
       }
    }

    _goNext();
  }

  _goNext() {
    //进入下一个界面
    // bool show = MkSpTool.showHomeGuide();
    // if (show) {
    //   Get.offNamed(MkAppRoutes.appGuide);
    // } else {
    //   Get.offNamed(MkAppRoutes.appTab);

    Get.offNamed(WJAppRoutes.appTab);
    }



    // ///zfz初始化埋点和归因 结束

  Future<bool> _pingTest(String url) async {
    try {
      final uri = Uri.parse(url);
      final request = await HttpClient().getUrl(uri).timeout(Duration(seconds: 3));
      final response = await request.close();
      return response.statusCode == 200;
    } catch (e) {
      print("Ping 异常：$e");
      return false;
    }
  }


}
